package aar.test.tessocr.base;


import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Locale;

import aar.test.tessocr.EventMessage.EventMessageBase;
import aar.test.tessocr.utils.ConstantPara;
import aar.test.tessocr.utils.LogUtils;
import aar.test.tessocr.utils.PrefUtils;
import butterknife.ButterKnife;

public class BaseActivity extends AppCompatActivity {
    AppManager appManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appManager = AppManager.getAppManager();
        // 把当前activity加入栈中
        appManager.addActivity(this);
        appManager.getSize("销毁前堆栈activity数目");
        changeLanguage();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // activity关闭，从栈中移除
        appManager.finishActivity(this);
        appManager.getSize("销毁后堆栈activity数目");
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        changeLanguage();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        changeLanguage();
    }

    private void changeLanguage() {

        String language = PrefUtils.getString(this, ConstantPara.CHANGE_LANGUAGE_PREF_KEY, "zh");
        LogUtils.e("language","language=="+language);
        // 本地语言设置
        Locale myLocale = new Locale(language);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetEventBusMessageOne(EventMessageBase eventMsg) {
        //空实现
    }



}

